//
//  NoteCacheDB.m
//  yuwei
//
//  Created by slz on 16/12/28.
//  Copyright © 2016年 youyuwei. All rights reserved.
//

#import "NoteCacheDB.h"
#import "DBHelper.h"

#define kFile_NoteDb                  [NSString stringWithFormat:@"%@/NoteCache.sqlite",kPATH_Workshop]


@interface NoteCacheDB()

@property (nonatomic,strong) FMDatabaseQueue* databaseQueue;

@end

@implementation NoteCacheDB

/** 持久化缓存 */
-(void)setKVNoteValue:(NSString*)value forKey:(NSString*)key andType:(NSString *)type
{
    if(key.length == 0){
        // key为空的，直接返回
        return;
    }
    
//    if (!value) {
//        // 清除已存在的
//        [self.databaseQueue inDatabase:^(FMDatabase *db) {
//            [DBHelper executeNoteDeleteForKey:key database:db];
//        }];
//    }else{
        // 先插入记录，再更新时间戳
        [self.databaseQueue inDatabase:^(FMDatabase *db) {
            [DBHelper insertDailyJsonString:value andKey:key type:type WithDatabase:db];
        }];
//    }
}


+(void)setValue:(NSString*)value forKey:(NSString*)key andType:(NSString *)type

{
    [[self defaultNoteDB] setKVNoteValue:value forKey:key andType:type];
}


+(NSString*)valueForKey:(NSString*)key
{
    return [[self defaultNoteDB] getKVNoteValueForKey:key];
}

- (NSArray *)getAllIdArray{
    __block NSArray* arr = nil;
    
    [self.databaseQueue inDatabase:^(FMDatabase *db) {
        arr = [DBHelper getAllJsonIdWith:db];
    }];
    return arr;
}

- (NSArray *)getAllNoteArray{
    __block NSArray* arr = nil;
    
    [self.databaseQueue inDatabase:^(FMDatabase *db) {
        arr = [DBHelper getAllJsonStringWith:db];
    }];
    return arr;
}

- (void)deleteNoteWithNoteId:(NSString *)noteId{
    [self.databaseQueue inDatabase:^(FMDatabase *db) {
        [DBHelper deleteNoteItem:noteId withDatabase:db];
    }];
}

-(void)updateNoteWithNoteId:(NSString *)noteId andNoteJson:(NSString *)noteJson{
    [self.databaseQueue inDatabase:^(FMDatabase *db) {
        [DBHelper updatedownJsonStringWithCurrentId:noteId andNewJsonString:noteJson andDataBase:db];
    }];
}

/** 返回缓存的value对象 */
- (NSString *)typeFromId:(NSString *)noteId{
    __block NSString* retStr = nil;
    [self.databaseQueue inDatabase:^(FMDatabase *db) {
       retStr= [DBHelper getTypeStringWithNoteId:noteId andDataBase:db];
    }];
    return retStr;
}

-(NSString*)getKVNoteValueForKey:(NSString*)key
{
    __block NSString* retStr = nil;
    
    [self.databaseQueue inDatabase:^(FMDatabase *db) {
        FMResultSet* retSet = [DBHelper executeNoteSelectValueForKey:key database:db];
        
        while ([retSet next]) {
            retStr = [retSet stringForColumn:kTableColumn_daily_json];
            break;
        }
    }];
    
    return retStr;
}

+(NoteCacheDB*)defaultNoteDB
{
    static NoteCacheDB* _defaultCacheDB = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _defaultCacheDB = [[NoteCacheDB alloc] init];
    });
    
    return _defaultCacheDB;
}


-(id)init
{
    self = [super init];
    if(self)
    {
        self.databaseQueue = [FMDatabaseQueue databaseQueueWithPath:[[self class] database]];
        
        // 创建缓存库
        [self createCacheDB];
    }
    
    return self;
}

+ (NSString*)database
{
    return kFile_NoteDb;
}

-(void)dealloc
{
    [self.databaseQueue close];
    self.databaseQueue = nil;
}

/** 创建缓存库 */
-(void)createCacheDB
{
    @try {
        // 创建缓存库
        [self.databaseQueue inDatabase:^(FMDatabase *db) {
            [DBHelper executeNoteCacheCreateDatabase:db];
        }];
    }
    @catch (NSException *exception) {
        //
        [self.databaseQueue close];
    }
    @finally {
        
    }
}
@end
